package ld.oauth.blog.controller.user;


import com.ld.common.ResultInfoFactory;
import ld.oauth.blog.model.po.user.SysPermission;
import ld.oauth.blog.service.user.SysPermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/permission")
@Slf4j
public class SysPermissionController {


    @Autowired
    private SysPermissionService sysPermissionService;


    @PreAuthorize("hasAuthority('sys:permission:list')")
    @PostMapping("/list")
    @ResponseBody
    public ResultInfoFactory list(){
        List<SysPermission> list = sysPermissionService.list();
        return ResultInfoFactory.success(list);
    }


    @PreAuthorize("hasAnyAuthority('sys:permission:add','sys:permission:edit')")
    @RequestMapping(value = "",method = {RequestMethod.POST,RequestMethod.PUT})
    public String saveOrUpdate(SysPermission sysPermission){
        sysPermissionService.saveOrUpdate(sysPermission);
        return "redirect:/permission";
    }

    @PreAuthorize("hasAuthority('sys:permission:delete')")
    @DeleteMapping("/{id}")
    @ResponseBody
    public ResultInfoFactory deleteById(@PathVariable Long id){
        sysPermissionService.deleteById(id);
        return ResultInfoFactory.success();
    }



}
